/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package com.isisufes.joinus.appserver;

import java.lang.Integer;
import java.sql.*;
import java.util.*;

public class Profile extends Servlet1
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -8191727773132489581L;

	private static String[] fields1 = {
        "IM1Server", "IM2Server", "priEmail", "secEmail1", "secEmail2",
        "secEmail3", "IM1user", "IM2user", "homePhone", "cellphone", "addr1",
        "addr2", "city", "state", "zipCode",

        "relationshipStatus", "birthYear", "birthDay", "birthMonth", "country",
        "isMale", "firstName", "lastName",

        "heigth", "eyeColor", "hairColor", "build", "looks", "bestFeat",
        "headline", "firstThing", "turnOffs", "firstDate", "pastRelation",
        "fiveThi", "bedroom", "idealMatch",

        "education", "degree", "industry", "subInd", "highSchool", "college",
        "major", "occupation", "company", "companyWeb", "title", "workemail",
        "workphone", "jobDesc", "careerSkills", "careerInt", "year",

        "children", "ethnicity", "religion", "polView", "sexOri", "smoking",
        "drinking", "pets", "hometown", "webpage", "aboutMe", "passions",
        "sports", "activities", "books", "music", "tvShows", "movies",
        "cuisines" };

    private static String[] fields2 = { "firstName", "lastName" };

    private PreparedStatement ps_exists, ps_get, ps_getp;

    protected void initStmt()
    {
        ps_exists = DBConn.stmt("select * from Profile where login=?");
        ps_get = DBConn.stmt("select * from Profile where login=?");
        ps_getp = DBConn.stmt("select * from Profile where idProfile=?");
    }

    @SuppressWarnings("unused")
	private boolean exists(String login) throws Exception
    {
        ps_exists.clearParameters();
        ps_exists.setString(1, login);

        ResultSet rs = ps_exists.executeQuery();
        boolean ret = rs.next();

        rs.close();
        return ret;
    }

    private String update(String id, String xml) throws Exception
    {
        Map<String,String> data = Parser.parse(xml);

        for (String field: fields1) {
            String value = data.get(field);
            if (value == null) continue;

            String query = "update Profile set "+field+"=? where idProfile=?";
            PreparedStatement ps = DBConn.stmt(query);

            ps.setString(1, value);
            ps.setInt(2, Integer.parseInt(id));  // .setString(2, id);
            ps.executeUpdate();
            ps.close();
        }

        return "";
    }

    private String strProfile(ResultSet rs, String[] fields) throws Exception
    {
        String[] values = new String[fields.length];

        if (!rs.next()) throw new Exception("User not found");

        int i = 0;

        for (String field: fields) {
            values[i++] = rs.getString(field);
            if (values[i-1] == null) values[i-1] = "";
        }

        rs.close();

        return Parser.encapsulate(fields, values);
    }

    private String get(String request) throws Exception
    {
        ps_get.clearParameters();
        ps_get.setString(1, request);

        return strProfile(ps_get.executeQuery(), fields1);
    }

    private String getProfile(String idProfile, String[] fields) throws Exception
    {
        ps_getp.clearParameters();
        ps_getp.setInt(1, Integer.parseInt(idProfile));  // .setString(1, idProfile);

        return strProfile(ps_getp.executeQuery(), fields);
    }

    protected String dispatch(String action) throws Exception
    {
        String ret = null;

        if (action.equals("get")) ret = get(arg("request"));
        if (action.equals("update")) ret = update(id, arg("xml"));
        if (action.equals("getProfile")) ret = getProfile(arg("idProfile"), fields1);
        if (action.equals("getProfileLite")) ret = getProfile(arg("idProfile"), fields2);

        return ret;
    }
}
